package com.imooc.reader.controller;

import com.imooc.reader.entity.Evaluation;
import com.imooc.reader.entity.Member;
import com.imooc.reader.service.Exception.BussinessException;
import com.imooc.reader.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class MemberController {

    @Autowired
    private MemberService memberService;

    @GetMapping("/register.html")
    public ModelAndView showReigister() {
        return new ModelAndView("/register");
    }

    @PostMapping("/registe")
    @ResponseBody
    public Map register(String vc, String username, String password, String nickname, HttpServletRequest request) {
        //获取前台正确的验证码
        String kaptchaVerifyCode = (String) request.getSession().getAttribute("kaptchaVerifyCode");

        //判断当前输入的和正确的验证码是否一致
        Map result = new HashMap();
        if (vc == null || kaptchaVerifyCode == null || !vc.equalsIgnoreCase(kaptchaVerifyCode)) {
            result.put("code", "1");
            result.put("msg", "验证码错误");
        } else {
            //验证码验证正确，再次添加用户信息
            try {
                memberService.creatMember(username, password, nickname);
                result.put("code", "0");
                result.put("msg", "success");
            } catch (BussinessException e) {
                result.put("code", e.getCode());
                result.put("msg", e.getMessage());
            }
        }
        return result;
    }

    @GetMapping("login.html")
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView("/login");
        return modelAndView;
    }


    /**
     * 登录校验
     */
    @PostMapping("/check_login")
    @ResponseBody
    public Map checkLogin(String username, String password, String vc, HttpSession session) {
        //先对验证码进行验证
        //获取前台正确的验证码
        String kaptchaVerifyCode = (String) session.getAttribute("kaptchaVerifyCode");

        //判断当前输入的和正确的验证码是否一致
        Map result = new HashMap();
        if (vc == null || kaptchaVerifyCode == null || !vc.equalsIgnoreCase(kaptchaVerifyCode)) {
            result.put("code", "1");
            result.put("msg", "验证码错误");
        } else {
            //验证码正确，对账户密码·进行校验
            try {
                Member member = memberService.checkLogin(username, password);
                result.put("code", "0");
                result.put("msg", "success");
            } catch (BussinessException e) {
                e.printStackTrace();
                result.put("code", e.getCode());
                result.put("msg", e.getMessage());
            }
        }
        return result;
    }

    /**
     * 写短评
     */
    public Map insertContort(String content, Long bookId, Long memberId, Integer score) {
        HashMap result = new HashMap();

        try {
            Evaluation evaluation = memberService.insertScore(bookId, memberId, score, content);
            result.put("code", "1");
            result.put("msg", "success");
        } catch (BussinessException e) {
            e.printStackTrace();
            result.put("code", e.getCode());
            result.put("msg", e.getMessage());
        }
        return result;


    }
}
